package top.jnaw.jee.utils.baidu;

import com.jfinal.kit.PropKit;
import java.net.URLEncoder;

/**
 * 身份证识别
 */
public class Idcard {


  public static String getIdcard(String filePath, String side) {
    String result = "";
    // 身份证识别url
    String idcardIdentificate = PropKit.get("baidu_idCard_url");
    // 本地图片路径
//    String filePath = "D:/images/front.png";
    try {
      byte[] imgData = FileUtil.readFileByBytes(filePath);
      String imgStr = Base64Util.encode(imgData);
      // 识别身份证正面id_card_side=front;识别身份证背面id_card_side=back;
      String params =
          "id_card_side=" + side + "&" + URLEncoder.encode("image", "UTF-8")
              + "="
              + URLEncoder.encode(imgStr, "UTF-8");
      /**
       * 线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
       */
      String accessToken = AuthService.getAuth();
      result = HttpUtil.post(idcardIdentificate, accessToken, params);
//      Log.i(result);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return result;
  }
}
